﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;
namespace RentalSystem.Data.EF.Dao
{
    public class EFPersonsDao : IPersonsDao
    {
        #region Queries

        private readonly string GetQuery;

        #endregion

        private EFManager<Persons> manager;

        public EFPersonsDao(RentalDatabaseEntities repository)
        {
            GetQuery = "SELECT VALUE p FROM Persons AS p WHERE p.PersonGuid = @Guid;";
            this.manager = new EFManager<Persons>(repository);
        }

        #region IDao1Key

        public Persons Add(Persons entity)
        {
            manager.Database.AddToPersons(entity);
            manager.Save();
            return entity;
        }

        public Persons Get(string id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Guid", id)
                };

            var persons = manager.SQLQueryToEntity(GetQuery, parameters);

            foreach (Persons person in persons)
            {
                return person;
            }

            return null;
        }

        public void Delete(string id)
        {
            throw new NotImplementedException();
        }

        public void Delete(Persons entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Persons entity, uint fields)
        {
            Persons fromDb = Get(entity.PersonGuid);

            if (fromDb != null)
            {
                if ((fields & (uint)PersonFields.Adress) == (uint)PersonFields.Adress)
                    fromDb.PersonAdress = entity.PersonAdress;
                if ((fields & (uint)PersonFields.BirthDate) == (uint)PersonFields.BirthDate)
                    fromDb.PersonBirthDate = entity.PersonBirthDate;
                if ((fields & (uint)PersonFields.City) == (uint)PersonFields.City)
                    fromDb.PersonCity = entity.PersonCity;
                if ((fields & (uint)PersonFields.Country) == (uint)PersonFields.Country)
                    if(!string.IsNullOrEmpty(entity.Country))
                        fromDb.Country = entity.Country;
                if ((fields & (uint)PersonFields.FirstName) == (uint)PersonFields.FirstName)
                    fromDb.PersonFirstName = entity.PersonFirstName;
                if ((fields & (uint)PersonFields.LastName) == (uint)PersonFields.LastName)
                    fromDb.PersonLastName = entity.PersonLastName;
                if ((fields & (uint)PersonFields.NationalIdentifier) == (uint)PersonFields.NationalIdentifier)
                    fromDb.PersonNationalIdentifier = entity.PersonNationalIdentifier;
                if ((fields & (uint)PersonFields.ZipCode) == (uint)PersonFields.ZipCode)
                    fromDb.PersonZipCode = entity.PersonZipCode;
            }
            manager.Save();
        }

        public List<Persons> List()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
